package dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import model.ShareUser;

public class UtilisateurDAO extends DAO<ShareUser>{

	List<ShareUser> listUser = new ArrayList<ShareUser>();

	public UtilisateurDAO() {
		super();
		listUser = getAllUser();
	}

	public List<ShareUser> getListUser() {
		return listUser;
	}

	public void setListUser(List<ShareUser> listUser) {
		this.listUser = listUser;
	}

	/*
	 * Cette methode permet d'ajouter un User à la base
	 */
	public void addUser(String email, String login, String mdp) {

		EntityManager em = DAO.getEm();
		EntityTransaction t = em.getTransaction();

		if (t.isActive()) {
			t.rollback();
		}

		ShareUser user = new ShareUser();

		user.setMailUser(email);
		user.setLogin(login);
		user.setPassword(mdp);
		t.begin();
		em.persist(user);
		t.commit();

	}

	/*
	 * methode qui permet de verifier si les identifiants saisis par un
	 * User sont correts ou non
	 */

	public ShareUser getUser(String login, String password) {
		ShareUser User = null;
		
		EntityManager em = DAO.getEm();

		String query = "SELECT u FROM ShareUser u WHERE u.login= :login and u.password =: password";

		Query requete = em.createQuery(query);
		requete.setParameter("login", login);
		requete.setParameter("password", password);
		
		try {
			User = (ShareUser) requete.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
		return User;
	}

	/*
	 * methode qui renvoi un User via son id
	 */

	public ShareUser getUserById(int idUser) {
		ShareUser user = null;
		
		EntityManager em = DAO.getEm();

		String query = "SELECT u FROM ShareUser u WHERE u.idUser =: idUser";

		Query requete = em.createQuery(query);
		requete.setParameter("idUser", idUser);

		try {
			user = (ShareUser) requete.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
		return user;
	}
	
	/*
	 * methode qui renvoi un User via son id
	 */

	public ShareUser getUserByLogin(String login) {
		ShareUser User = null;
		
		EntityManager em = DAO.getEm();

		String query = "SELECT u FROM ShareUser u WHERE u.login =: login";

		Query requete = em.createQuery(query);
		requete.setParameter("login", login);

		try {
			User = (ShareUser) requete.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
		return User;
	}

	/*
	 * cette méthode permet de lister tous les Users présents dans la
	 * base
	 */
	public List<ShareUser> getAllUser() {
		List<ShareUser> listUser = new ArrayList<ShareUser>();
		
		EntityManager em = DAO.getEm();

		String query = "SELECT u FROM ShareUser u";

		TypedQuery<ShareUser> requete = (TypedQuery<ShareUser>) em.createQuery(query, ShareUser.class);
		listUser = requete.getResultList();

		return listUser;
	}

	/*
	 * cette méthode permet de supprimer un User
	 */
	public void deleteUser(int idUser) {

		EntityManager em = DAO.getEm();
		EntityTransaction t = em.getTransaction();

		if (t.isActive()) {
			t.rollback();
		}

		t.begin();

		ShareUser su=em.find(ShareUser.class, idUser);
		em.remove(su);
		
		t.commit();

	}

	/*
	 * Cette methode permet de modifier un User à la base
	 */
	public void changeUser(int idUser, String email, String login, String mdp) {

		EntityManager em = DAO.getEm();
		EntityTransaction t = em.getTransaction();

		if (t.isActive()) {
			t.rollback();
		}

		ShareUser user = em.find(ShareUser.class, idUser);
		user.setLogin(login);
		user.setMailUser(email);
		user.setPassword(mdp);

		t.begin();
		em.merge(user);
		t.commit();
		
	}

}
